import { ChunkEntityType, ChunkEntityUpdateType, ColliderType, CollisionGroup } from "../../GameDef";
import { CircleShape } from "../../lib/kgame/d2/shape/CircleShape";
import { ECSScene } from "../../lib/kgame/ecs/ECSScene";
import { IVec2 } from "../../lib/kgame/util/Defined";
import { AICmp } from "../cmp/AICmp";
import { ChunkEntityCmp } from "../cmp/ChunkEntityCmp";
import { ColliderCmp } from "../cmp/ColliderCmp";
import { MoveCmp } from "../cmp/MoveCmp";
import { SnakeBodyCmp } from "../cmp/SnakeBodyCmp";
import { SnakeCmp } from "../cmp/SnakeCmp";
import { SnakeHeadCmp } from "../cmp/SnakeHeadCmp";
import { SnakeAI } from "../cmp/ai/SnakeAI";
import { WorldMgr } from "../scmp/WorldMgr";

/**
 * 蛇工厂
 */
export class SnakeFactory {

    /**
     * 创建蛇
     * @param scene 场景
     * @param snakeId 蛇id
     */
    static createSnake(scene: ECSScene, name: string) {
        const entity = scene.createEntity();
        const snakeId = scene.getCmp(WorldMgr).getSnakeId();
        const snake = entity.addCmp(SnakeCmp).onCreate(snakeId, name);
        return snake;
    }

    /**
     * 创建ai蛇
     * @param scene 场景
     * @param snakeId 蛇id
     */
    static createAiSnake(scene: ECSScene) {
        const snake = this.createSnake(scene, scene.getCmp(WorldMgr).getAIName());
        snake.addCmp(AICmp).onCreate(new SnakeAI(snake));
        return snake;
    }

    /**
     * 创建蛇头
     * @param snake 蛇组件
     */
    static createHead(snake: SnakeCmp) {
        const entity = snake.scene.createEntity();
        entity.pos.setPosTo(snake.pos);
        entity.rot = snake.rot;

        const head = entity.addCmp(SnakeHeadCmp);
        const foodCollider = entity.addCmp(ColliderCmp<CircleShape>).onCreate(new CircleShape(0), ColliderType.none, CollisionGroup.eatFood, snake.snakeId);
        const snakeCollider = entity.addCmp(ColliderCmp<CircleShape>).onCreate(new CircleShape(0), ColliderType.none, CollisionGroup.snake, snake.snakeId);
        head.onCreate(snake, entity.addCmp(MoveCmp).onCreate(entity.rot), foodCollider, snakeCollider);

        return head;
    }

    /**
     * 创建蛇身
     * @param head 蛇头组件
     */
    static createBody(head: SnakeHeadCmp, pos?: IVec2) {
        const idx = head.bodys.length;
        const entity = idx === 0 ? head.entity : head.scene.createEntity();
        const body = entity.addCmp(SnakeBodyCmp);
        const collider = entity.addCmp(ColliderCmp<CircleShape>).onCreate(new CircleShape(0), ColliderType.snake, CollisionGroup.none, head.snake.snakeId);
        const last = head.tail;
        head.bodys.push(body.onCreate(head, collider));

        if (idx === 0) {
            body.pos.setPosTo(pos);
            body.entity.rot = head.entity.rot;
        } else {
            if (pos) {
                head.pathTail.next = { x: pos.x, y: pos.y, rot: last.entity.rot };
            }
            body.pos.setPosTo(pos || last.pos);
            body.entity.rot = last.entity.rot;
        }

        entity.addCmp(ChunkEntityCmp).onCreate(ChunkEntityType.snakeBody, ChunkEntityUpdateType.update, collider.shape.worldAABB);
        return body;
    }

}